package com.chengyu.core.service.dataLog.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.AccountEnums;
import com.chengyu.core.domain.enums.ActionTypeEnums;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.ConfigAccountMapper;
import com.chengyu.core.mapper.CustomConfigAccountMapper;
import com.chengyu.core.mapper.SysActionDataLogMapper;
import com.chengyu.core.model.*;
import com.chengyu.core.service.config.ConfigAccountService;
import com.chengyu.core.service.dataLog.ActionDataLogService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @title  操作数据记录
 * @author LeGreen
 * @date   2021/1/6
 */
@Service
public class ActionDataLogServiceImpl implements ActionDataLogService {
	
	@Autowired
	private SysActionDataLogMapper sysActionDataLogMapper;


	@Override
	public void add(SysActionDataLog sysActionDataLog) throws ServiceException {
		if (sysActionDataLog.getAppType()==null){
			throw new ServiceException("操作类型来源不能为空");
		}else{
			if (sysActionDataLog.getAppType().equals(ActionTypeEnums.AppTypeTypes.SHOP.getValue()) &&sysActionDataLog.getShopId()==null){
				throw new ServiceException("当前为商家端，商家ID不能为空");
			}
			if ((sysActionDataLog.getAppType().equals(ActionTypeEnums.AppTypeTypes.SYS.getValue()) ||
					sysActionDataLog.getAppType().equals(ActionTypeEnums.AppTypeTypes.PORTAL.getValue())) &&
					sysActionDataLog.getActionMemberId()==null){
				throw new ServiceException("当前为平台后台或用户端，使用者ID不能为空");
			}
		}
		//先删除
		SysActionDataLogExample example = new SysActionDataLogExample();
		SysActionDataLogExample exampleResult = authParams(sysActionDataLog, example);
		sysActionDataLogMapper.deleteByExample(exampleResult);
		sysActionDataLog.setStatus(CommonConstant.YES_INT);
		sysActionDataLog.setAddTime(DateUtil.date());
		sysActionDataLogMapper.insertSelective(sysActionDataLog);
	}



	@Override
	public SysActionDataLog get(SysActionDataLog sysActionDataLog) throws ServiceException {
		if (sysActionDataLog.getAppType()==null) {
			throw new ServiceException("操作类型来源不能为空");
		}
		SysActionDataLogExample example = new SysActionDataLogExample();
		SysActionDataLogExample exampleResult = authParams(sysActionDataLog, example);

		List<SysActionDataLog> sysActionDataLogList = sysActionDataLogMapper.selectByExampleWithBLOBs(exampleResult);
		if (sysActionDataLogList.size()>0) {
			return sysActionDataLogList.get(0);
		}else{
			return null;
		}

	}

	private SysActionDataLogExample authParams(SysActionDataLog sysActionDataLog,SysActionDataLogExample example) {
		SysActionDataLogExample.Criteria criteria = example.createCriteria();
		if (sysActionDataLog.getShopId()!=null){
			criteria.andShopIdEqualTo(sysActionDataLog.getShopId());
		}
		if (sysActionDataLog.getAppType()!=null){
			criteria.andAppTypeEqualTo(sysActionDataLog.getAppType());
		}
		if (sysActionDataLog.getActionLocation()!=null){
			criteria.andActionLocationEqualTo(sysActionDataLog.getActionLocation());
		}
		if (sysActionDataLog.getActionMemberId()!=null){
			criteria.andActionMemberIdEqualTo(sysActionDataLog.getActionMemberId());
		}
		if (sysActionDataLog.getStatus()!=null){
			criteria.andStatusEqualTo(sysActionDataLog.getStatus());
		}
		return example;
	}
}